/*
 * @Author: zzr
 * @Date: 2025-03-03 14:41:06
 * @LastEditors: zzr
 * @Description: 
 */
// // * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// // * Copyright 2023 The Geek-AI Authors. All rights reserved.
// // * Use of this source code is governed by a Apache-2.0 license
// // * that can be found in the LICENSE file.
// // * @Author yangjian102621@163.com
// // * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

import axios from "axios";
import {
  getAdminToken,
  getSessionId,
  getUserToken,
  removeAdminToken,
  removeUserToken,
} from "@/store/session";
// import { MessageBox, Message, Loading } from "element-ui";
import { ElMessage } from "element-plus";
axios.defaults.timeout = 180000;
// axios.defaults.baseURL = "http://172.16.81.120:48080"
axios.defaults.baseURL = import.meta.env.VITE_APP_BASE_API;
axios.defaults.withCredentials = true;
axios.defaults.headers.post["Content-Type"] = "application/json";

// HTTP拦截器
axios.interceptors.request.use(
  (config) => {
    // set token
    config.headers["Chat-Token"] = getSessionId();
    config.headers["Authorization"] =
      config.headers.Authorization || getUserToken();
    config.headers["Admin-Authorization"] = getAdminToken();
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);
axios.interceptors.response.use(
  (response) => {
    let { data } = response;
    if (data.code != 0) {
      if (data.code === 401) {
        removeUserToken();
        ElMessage({
          message: data.msg,
          type: "warning",
        });
      } else {
        ElMessage({
          message: data.msg,
          type: "error",
        });
      }
      return Promise.reject(data.msg || "Error");
    } else {
      // console.log('res :>> ', response.data);
      return response.data;
    }
  },
  (error) => {
    // || error.response.status === 400
    if (error.response.status === 401) {
      if (error.response.request.responseURL.indexOf("/api/admin") !== -1) {
        removeAdminToken();
      } else {
        removeUserToken();
      }
      error.response.data.message = "请先登录";
      return Promise.reject(error.response.data);
    }
    if (error.response.status === 400) {
      return Promise.reject(new Error(error.response.data.message));
    } else {
      return Promise.reject(error);
    }
    return Promise.reject(error);
  }
);

// send a http get request
export function httpGet(url, params = {}) {
  console.log('%chttpGet改掉 不允许使用','color: white; background-color: #f1738e; padding: 2px 5px; border-radius: 2px')
  return new Promise((resolve, reject) => {
    axios
    .get(url, {
      params: params,
    })
    .then((response) => {
      resolve(response.data);
    })
    .catch((err) => {
      reject(err);
    });
  });
}

// send a http post request
export function httpPost(url, data = {}, options = {}) {
  console.log('%chttpPost改掉 不允许使用','color: white; background-color: #f1738e; padding: 2px 5px; border-radius: 2px')
  return new Promise((resolve, reject) => {
    axios
      .post(url, data, options)
      .then((response) => {
        resolve(response.data);
      })
      .catch((err) => {
        reject(err);
      });
  });
}
export default axios;
